/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.templates.pet;

import java.util.Arrays;

/**
 * @author Rolandas
 */
public class PetDopingBag {

    private int[] itemBag = null;
    private boolean isDirty = false;

    public void setFoodItem(int itemId) {
        setItem(itemId, 0);
    }

    public int getFoodItem() {
        if (itemBag == null || itemBag.length < 1)
            return 0;
        return itemBag[0];
    }

    public void setDrinkItem(int itemId) {
        setItem(itemId, 1);
    }

    public int getDrinkItem() {
        if (itemBag == null || itemBag.length < 2)
            return 0;
        return itemBag[1];
    }

    /**
     * Adds or removes item to the bag
     *
     * @param itemId - item Id, or 0 to remove
     * @param slot   - slot number; 0 for food, 1 for drink, the rest are for scrolls
     */
    public void setItem(int itemId, int slot) {
        if (itemBag == null) {
            itemBag = new int[slot + 1];
            isDirty = true;
        } else if (slot > itemBag.length - 1) {
            itemBag = Arrays.copyOf(itemBag, slot + 1);
            isDirty = true;
        }
        if (itemBag[slot] != itemId) {
            itemBag[slot] = itemId;
            isDirty = true;
        }
    }

    public int[] getScrollsUsed() {
        if (itemBag == null || itemBag.length < 3)
            return new int[0];
        return Arrays.copyOfRange(itemBag, 2, itemBag.length);
    }

    /**
     * @return true if the bag needs saving
     */
    public boolean isDirty() {
        return isDirty;
    }

}
